const getStudentHomeworkStatusListUrl = require('../../../../../config.js').getStudentHomeworkStatusListUrl
const getCLassList = require('../../../../../config.js').getCLassList // 获取班级ID
const getStudentListUrl = require('../../../../../config.js').getStudentList // 获取今日作业学生
const addSubjectComplete = require('../../../../../config').addSubjectComplete // 确认这个小姑娘完成作业了

let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		comment_status:'未辅导',
		studentList:[],
		color:'red',
    classListArray:null,
		index:0,
		currentRightId:0,
		width:0, // 弹出的宽度
  },
	onLoad(){
		let templateArray = this.data.studentList
		templateArray.forEach((item,index) => {
			switch(item.type){
				case 0:
					item.color = 'red'
					break;
				case 1:
					item.color = 'yellow'
					break;
				case 2:
					item.color = 'green'
					break;
			}
		})
		this.setData({
			studentList : templateArray
		})
    let that = this
    wx.getStorage({
      key: 'teacherInfo',
      success: function (res) {
        let organizationId = res.data.organizationId
        let staffId = res.data.staffId
        let parms = {
          classType: 'TRAIN',
          teacherId: staffId
        }
        that.apiGetClassId(parms)
      },
    })
    console.log(this.data.classListArray)
	},
  //api 1.查、获取辅导班 2.查、获取学生bYclassId 3.增、确认完成作业
  apiGetClassId(parms){
    wx.request({
      url: getCLassList,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: parms,
      success: (res) => {
        console.log(res.data.data)
        let classListArray = res.data.data
        this.setData({
          classListArray: classListArray
        })
        let parms = {
          classId: res.data.data[0].classId
        }
        this.apiGetStudentList(parms)
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  apiGetStudentList(parms){
    wx.request({
      url: getStudentListUrl,
      method: 'POST',
      data: parms,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: (res) => {
        console.log(res)
        let studentList = []
        studentList = res.data.data
        studentList.forEach( (item,index) => {
          switch (item.homeworkStatus) {
            case 'BEFORE':
              item.color = '#f17e7e'
              break;
            case 'MINDDLE': 
              item.color = '#dfe38e'
              break;
            case 'AFTER':
              item.color = '#7fe7cc'
              break;
          }
        })
        console.log(studentList)
        this.setData({
          studentList: studentList
        })
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  apiAddCompete(parms){
    wx.request({
      url: addSubjectComplete,
      method: 'POST',
      data: parms,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: (res) => {
        console.log(res)
        if(res.data.status == 200){
          wx.showToast({
            title: '确认成功',
            icon: 'success',
            duration: 2000
          })
        }
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  // 查看已经添加的作业
	displayRight(e){
    console.log(e.currentTarget.dataset.staffinfo)
    let staffinfo = JSON.stringify(e.currentTarget.dataset.staffinfo)
		wx.navigateTo({
      url: "./learDe?staffinfo=" + staffinfo,
		})
	},
  // 飞到添加作业
	addWork(e){
    let staffinfo = JSON.stringify(e.currentTarget.dataset.staffinfo)
		wx.navigateTo({
      url: "./addWork?staffinfo=" + staffinfo,
		})
	},
	bindPickerChange(e) {
    let index = e.detail.value

    if(index == 0){
      console.log('我天庭成功')
    }
		this.setData({
      index: index
		})
	},
	popRight(e){
		console.log(e.currentTarget.dataset.id)
		let currentRightId = e.currentTarget.dataset.id
		this.setData({
			currentRightId : currentRightId,
			width : '235rpx'
		})
	},
	// 完成作业
  completeWork(e){
    console.log(e.currentTarget.dataset.staffinfo)
    let staffinfo = e.currentTarget.dataset.staffinfo
    let that = this
    wx.showModal({
      title: '提示',
      content: `确认${staffinfo.studentName}完成了所有作业吗？`,
      success(res) {
        if (res.confirm) {
          let parms = {
            id: staffinfo.id
          }
          that.apiAddCompete(parms)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})